package com.hzcx.app.simplechat.ui.friend;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.friend.adapter.FriendRequestAdapter;
import com.hzcx.app.simplechat.ui.friend.bean.FriendRequestBean;
import com.hzcx.app.simplechat.ui.friend.contract.FriendRequestContract;
import com.hzcx.app.simplechat.ui.friend.event.AddFriendSuccessEvent;
import com.hzcx.app.simplechat.ui.friend.presenter.FriendRequestPresenter;
import com.hzcx.app.simplechat.ui.home.AddFriendActivity;
import com.hzcx.app.simplechat.ui.home.AddFriendFirstActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 好友请求界面
 */
public class FriendRequestActivity extends BaseActivity<FriendRequestPresenter> implements FriendRequestContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_friend_request)
    RecyclerView rvFriendRequest;

    private List<FriendRequestBean> requestData;
    private FriendRequestAdapter requestAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_friend_request;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FriendRequestPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("好友请求");
        ivTitleRight.setImageResource(R.mipmap.ic_icon_freind_add);

        requestData = new ArrayList<>();
        requestAdapter = new FriendRequestAdapter(requestData);
        rvFriendRequest.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFriendRequest.setAdapter(requestAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getFriendRequestList(this);

        requestAdapter.setOnAgreeFriendRequestListener(new FriendRequestAdapter.OnAgreeFriendRequestListener() {
            @Override
            public void agree(int headerPos, int contentPos, int id) {
                mPresenter.agreeFriendRequest(FriendRequestActivity.this, id, headerPos, contentPos);
            }
        });
    }

    @OnClick(R.id.iv_title_right)
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_right:
                startActivity(new Intent(this, AddFriendFirstActivity.class));
                break;
        }
    }

    /**
     * 好友请求列表获取成功
     *
     * @param data
     */
    @Override
    public void friendRequestResult(List<FriendRequestBean> data) {
        try {
            requestData.addAll(data);
            requestAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 同意好友请求成功
     *
     * @param headPos
     * @param contentPos
     */
    @Override
    public void agreeSuccess(int headPos, int contentPos) {
        requestData.get(headPos).getOne_data().get(contentPos).setState(1);
        requestAdapter.notifyDataSetChanged();
        EventBus.getDefault().post(new AddFriendSuccessEvent());
    }
}
